#ifndef LIGHTCONTROL_H
#define LIGHTCONTROL_H

#pragma once

#include <QObject>
#include <QTimer>
#include <QProcess>
#include <QThread>

namespace ns_yht
{
    namespace LightColor
    {
        const int Red = 0;
        const int Green = 1;
        const int Blue = 2;
    }
#define LIGHTTASK LightTask::instance()
    typedef QPair<int, bool> LightState; // 开(1)关(0)未设置(-1)、闪烁(true)

    class LightControl : public QObject
    {
        Q_OBJECT
    public:
        LightControl();
        ~LightControl();

        void start();

        friend class LightTask;

    private slots:
        void turnOn(int lightId, bool on);
        // void firstTurnOn(int lightId, bool on);

        void startShrink(int lightId);
        void stopShrink(int lightId);

        void onProcessError(QProcess::ProcessError error);
        void onProcessMuteError(QProcess::ProcessError error);
    private slots:
        void onTimer();

    signals:
        void ensureCurrent();

    private:
        void processCommand(int lightId, int command);

        void detectMute();

        LightState m_lightStates[3];
        int m_newStates[3];
        QTimer *m_timer{nullptr};
        QProcess *m_process{nullptr};
        QProcess *m_processMute{nullptr};
    };

    class LightTask : public QObject
    {
        Q_OBJECT
    public:
        LightTask(QObject *parent = nullptr);
        ~LightTask();

        static LightTask *instance();

        void start();

        void turnOn(int lightId, bool on);
        // void firstTurnOn(int lightId, bool on);

        void startShrink(int lightId);
        void stopShrink(int lightId);

    signals:
        void signalOfTurnOn(int lightId, bool on);
        // void signalOfFirstTurnOn(int lightId, bool on);
        void signalOfStartShrink(int lightId);
        void signalOfStopShrink(int lightId);

        void signalOfStart();

    private:
        QThread m_thread;
        LightControl *m_lightControl;
    };

}

#endif